package com.weilan.blog.admin.service.impl;

import com.weilan.blog.admin.entity.OnlineAdmin;
import com.weilan.blog.admin.entity.SysAdmin;
import com.weilan.blog.admin.mapper.SysAdminMapper;
import com.weilan.blog.admin.service.SysAdminService;
import com.weilan.blog.admin.vo.SysAdminVO;
import com.weilan.blog.base.global.Constants;
import com.weilan.blog.base.holder.RequestHolder;
import com.weilan.blog.base.serviceImpl.SuperServiceImpl;
import com.weilan.blog.common.global.RedisConf;
import com.weilan.blog.common.global.SysConf;

import com.weilan.blog.utils.IpUtils;
import com.weilan.blog.utils.JsonUtils;
import com.weilan.blog.utils.RedisUtil;
import com.weilan.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/9/23 22:32
 */
@Service
public class SysAdminServiceImpl extends SuperServiceImpl<SysAdminMapper, SysAdmin> implements SysAdminService {


    @Autowired
    private RedisUtil redisUtil;

    @Override
    public SysAdmin getAdminByUid(String uid) {
        return null;
    }

    @Override
    public String getOnlineAdminList(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public SysAdmin getAdminByUser(String userName) {
        return null;
    }

    @Override
    public SysAdmin getMe() {
        return null;
    }

    @Override
    public void addOnlineAdmin(SysAdmin admin, Long expirationSecond) {
        HttpServletRequest request = RequestHolder.getRequest();
        Map<String, String> map = IpUtils.getOsAndBrowserInfo(request);
        String os = map.get(SysConf.OS);
        String browser = map.get(SysConf.BROWSER);
        String ip = IpUtils.getIpAddr(request);
        OnlineAdmin onlineAdmin = new OnlineAdmin();
        onlineAdmin.setAdminUid(admin.getUid());
        onlineAdmin.setTokenId(admin.getValidCode());
        onlineAdmin.setOs(os);
        onlineAdmin.setBrowser(browser);
        onlineAdmin.setIpaddr(ip);
        onlineAdmin.setLoginTime(LocalDateTime.now().toString());
        onlineAdmin.setRoleName(admin.getRole().getRoleName());
        onlineAdmin.setUserName(admin.getUserName());
        onlineAdmin.setExpireTime(LocalDateTime.now().minusMinutes(expirationSecond).toString());
        //从Redis中获取IP来源
        String jsonResult = redisUtil.get(RedisConf.IP_SOURCE + Constants.SYMBOL_COLON + ip);
        if (StringUtils.isEmpty(jsonResult)) {
            String addresses = IpUtils.getAddresses(SysConf.IP + SysConf.EQUAL_TO + ip, SysConf.UTF_8);
            if (StringUtils.isNotEmpty(addresses)) {
                onlineAdmin.setLoginLocation(addresses);
                redisUtil.setEx(RedisConf.IP_SOURCE + Constants.SYMBOL_COLON + ip, addresses, 24, TimeUnit.HOURS);
            }
        } else {
            onlineAdmin.setLoginLocation(jsonResult);
        }
        redisUtil.setEx(RedisConf.LOGIN_TOKEN_KEY + RedisConf.SEGMENTATION + admin.getValidCode(), JsonUtils.objectToJson(onlineAdmin), expirationSecond, TimeUnit.SECONDS);

    }

    @Override
    public String getList(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public String addAdmin(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public String editAdmin(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public String editMe(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public String changePwd(String oldPwd, String newPwd) {
        return null;
    }

    @Override
    public String resetPwd(SysAdminVO adminVO) {
        return null;
    }

    @Override
    public String deleteBatchAdmin(List<String> adminUids) {
        return null;
    }

    @Override
    public String forceLogout(List<String> tokenList) {
        return null;
    }
}
